package servlet;

import java.io.IOException;
import java.util.List;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import entities.Category;

import managers.manCategory;

/**
 * Servlet implementation class SevCategory
 */
public class SevCategory extends HttpServlet {
	private static final long serialVersionUID = 1L;
	@EJB
	manCategory manCat;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SevCategory() {
        super();
        // TODO Auto-generated constructor stub
        manCat = new manCategory();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html");
		String types=(String)request.getParameter("types");

		if ("add".equals(types)){
			System.out.println("addCate");
			String nom = (String)request.getParameter("name");
			boolean v=manCat.addCate(nom);
			
			String message="Vous n'avez pas reussi a creer le category :"+nom;
			if (v==true){
				message="Vous avez reussi a creer le category : "+nom;
			}
			request.setAttribute("message", message);
			List<Category> allCate=manCat.allCategory();
			request.setAttribute("allCate", allCate);
			request.getRequestDispatcher("/gererCategory.jsp").forward(request, response);
		}
		if ("del".equals(types)){
		
			String id=(String)request.getParameter("id");
			boolean v=manCat.delCategory(Integer.parseInt(id));
			String message="Vous n'avez pas reussi a supprimer le category !";
			if (v==true){
				message="Vous avez reussi a supprimer le category ! ";
			}
			request.setAttribute("message", message);
			List<Category> allCate=manCat.allCategory();
			request.setAttribute("allCate", allCate);
			request.getRequestDispatcher("/gererCategory.jsp").forward(request, response);
			
		}
		if ("update".equals(types)){
			String id=(String)request.getParameter("id");
			String nom = (String)request.getParameter("name");
			boolean v=manCat.updateCategory(Integer.parseInt(id),nom);
			String message="Vous n'avez pas reussi a maj le category !";
			if (v==true){
				message="Vous avez reussi a maj le category ! ";
			}
			List<Category> allCate=manCat.allCategory();
			request.setAttribute("allCate", allCate);
			request.setAttribute("message", message);
			request.getRequestDispatcher("/gererCategory.jsp").forward(request, response);
		}
		if ("allCate".equals(types)){
			List<Category> allCate=manCat.allCategory();
			request.setAttribute("allCate", allCate);
			request.getRequestDispatcher("/gererCategory.jsp").forward(request, response);
		}
		if ("choose".equals(types)){
			List<Category> allCate=manCat.allCategory();
			request.setAttribute("allCate", allCate);
			request.getRequestDispatcher("/chooseCategory.jsp").forward(request, response);
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request,response);
	}

}
